<template>
  <div>
    <TableBar title="课程列表">
      <el-button @click="onShowCreate" type="primary">新建课程</el-button>
    </TableBar>

    <table cellspacing="0" cellpadding="0" class="obj-con-table">
        <thead>
          <tr>
            <td>课程名称</td>
            <td>购买数量</td>
            <td>课程状态</td>
            <td>创建时间</td>
            <td>操作</td>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(c, i) in tableData" :key="'course' + i">
            <td  class="flex-row-center" @click="onShowDetail(c)" >
              <div class="table-list-item-avatar">
                <img v-if="c.avatar_udl" width="86" height="62" :src="c.avatar_url" />
                <div v-else>{{c.course_name.substring(0, 4)}}</div>
              </div>
              <div class="table-list-item-con-name">
                <p>{{c.course_name}}</p>
                <p class="price">￥{{c.price/100}}</p>
              </div>
            </td>
            <td>{{c.student_count}}</td>
            <td><status-show :status="c.status" /></td>
            <td>{{c.create_time}}</td>
            <td>
              <el-button @click="onShowEdit(c)" type="primary">编辑</el-button>
            </td>
          </tr>
        </tbody>
    </table>
    <el-pagination
      background
      layout="prev, pager, next, sizes, jumper"
      @current-change="handleCurrentChange"
      @size-change=handleSizeChange
      :current-page.sync="search.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="search.page_size"
      :total="total"
    ></el-pagination>
    <div class="padding-bottom"></div>

  </div>
</template>
<script>
import pageMixin from "@/mixins/pageMixin";
import TableBar from "@/components/TableBar";
import {courseModel} from "@/api/models"
import CreateFilter from "@/mixins/CreateFilter.js"
import StatusShow from '../components/StatusShow.vue';
export default {
  mixins: [pageMixin, CreateFilter],
  components: { TableBar, StatusShow },
  data() {
    return {
      tableData: [],
      total: 0,
      search: {
        course_id: null,
        course_name: null,
        teacher_name: null,
        course_type: null,
        course_status: null,
        page: 1,
        page_size: 10
      }
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    onShowCreate() {
      this.$router.push('/createCourse/')
    },
    onShowEdit(row) {
      this.$router.push('/editCourse/' + row.course_id)
    },
    onShowDetail(row) {
      this.$router.push('/course/' + row.course_id)
    },
    onDelete(row) {
      this.$confirm(`确定删除${row.course_name}`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
        }).catch((err) => {
          this.$message({
            type: 'info',
            message: err.message
          });          
        });
    },
    getList() {
      console.log("getList");
      courseModel.list(
        this.search,
        res => {
          this.total = res.total_records;
          this.tableData = res.data.map(v => {
            v.courseTypeLabel = this.courseTypeToLabel(v.course_type);
            v.teachers = v.teacher_set.map((v) => {
              return v.name
            }).join(',');
            return v;
          });
        },
        err => {
          console.log(err);
        }
      );
    }
  }
};
</script>
<style scope lang="scss">

</style>